package com.zhf.servlet;

import com.zhf.bean.Student;
import com.zhf.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/17 17:04
 */
@WebServlet(name = "GetStudentServlet",urlPatterns = "/getStudent")
public class GetStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object studentID = request.getParameter("studentID");
        System.out.println("查询学号："+studentID+" 的学生信息");
        //检查学生是否存在
        StudentService studentService = new StudentService();
        boolean isreal = studentService.isStudentID((String) studentID);
        if (isreal){
            Student student = studentService.getStudentByID((String) studentID);
            //将查到的学生写入session返回
            session.setAttribute("studentresult",student);
            response.sendRedirect("studentresultpage.jsp");
        }else {
            //服务器跳转request共享
            request.setAttribute("error","该学生不存在");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("studentinfo.jsp");
            requestDispatcher.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
